//
//  SousuoViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/5.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "SousuoViewController.h"

#import "HomeProductCollectionViewCell.h"
#import "ChanpinxiangqingViewController.h"

@interface SousuoViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UITextFieldDelegate>

@property (nonatomic, strong) UITextField * text_search;

//
@property (nonatomic, strong) UICollectionView * collectionView_product;
//数据
@property (nonatomic, strong) NSMutableArray * arr_data;

@end

@implementation SousuoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _lblTitle.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.backgroundColor = [UIColor blackColor];
    
    
    UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(15, 32, 10, 20)];
    image_return.image = [UIImage imageNamed:@"zuofanhui@2x.png"];
    [_topView addSubview:image_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 20, 35, 44);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];

    //搜索
    UIView * view_topSearch = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(btn_return.frame), 20 + 4.5, SCREEN_WIDTH - CGRectGetMaxX(btn_return.frame) - 50, 33.5)];
    view_topSearch.backgroundColor = [UIColor whiteColor];
    view_topSearch.layer.cornerRadius = 3;
    [_topView addSubview:view_topSearch];
    
    UIImageView * image_search = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(btn_return.frame) + 10, 20 + 12, 20, 20)];
    image_search.image = [UIImage imageNamed:@"sousuo@2x.png"];
    [_topView addSubview:image_search];
    
    self.text_search = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_search.frame) + 5, 20 + 14.5, SCREEN_WIDTH - CGRectGetMaxX(image_search.frame) - 8 - 50, 15)];
    self.text_search.delegate = self;
    self.text_search.returnKeyType = UIReturnKeySearch;
    self.text_search.placeholder = @"请输入您要搜索的内容";
    self.text_search.textColor = [UIColor blackColor];
    self.text_search.font = [UIFont systemFontOfSize:15];
    self.text_search.clearButtonMode = UITextFieldViewModeAlways;
    [self.text_search becomeFirstResponder];
    [_topView addSubview:self.text_search];

    //右侧栏
    UIButton * btn_image_erji = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_image_erji.frame = CGRectMake(SCREEN_WIDTH - 5 - 40 , 20, 40, 44);
    [btn_image_erji setTitle:@"搜索" forState:(UIControlStateNormal)];
    [btn_image_erji setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_image_erji.titleLabel.font = [UIFont systemFontOfSize:17];
    btn_image_erji.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_topView addSubview:btn_image_erji];
    [btn_image_erji addTarget:self action:@selector(btn_image_erjiAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//搜索
- (void )btn_image_erjiAction:(UIButton *)sender
{
//    NSLog(@"点击搜索");
    
    [self.text_search resignFirstResponder];
    
    self.arr_data = @[@"",@"",@""].mutableCopy;

    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.collectionView_product reloadData];
    });
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    //每个item的大小
    int  item_length = (SCREEN_WIDTH - 30) / 2;
    layout.itemSize = CGSizeMake(item_length, item_length + 55);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView_product = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    self.collectionView_product.delegate = self;
    self.collectionView_product.dataSource = self;
    self.collectionView_product.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.collectionView_product];
    
    //注册
    [self.collectionView_product registerClass:[HomeProductCollectionViewCell class] forCellWithReuseIdentifier:@"cell_product"];
}

#pragma mark - collectionView的代理
//有几个分区
- (NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个分区有多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr_data.count;    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeProductCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell_product" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    //
    [cell.image_product sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"cainixihuan@2x.png"]];
    
    return cell;
}

- (void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",(long)indexPath.item);
    
    ChanpinxiangqingViewController * chanpinxiangqingViewController = [[ChanpinxiangqingViewController alloc] init];
    [self showViewController:chanpinxiangqingViewController sender:nil];
}

#pragma mark - textField代理
- (BOOL )textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    self.arr_data = @[@"",@"",@""].mutableCopy;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.collectionView_product reloadData];
    });
    
    return YES;
}

#pragma mark - 懒加载
- (NSMutableArray *)arr_data
{
    if(_arr_data == nil)
    {
        self.arr_data = [NSMutableArray array];
    }
    
    return _arr_data;
}











@end
